package gov.nhso.prov.acc.apps.acc6;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.isud.core.ISUD;
import com.isud.core.ISUDFactory;

import gov.nhso.prov.acc.beans.Acc6ProvinceAccountBean;
import gov.nhso.prov.acc.core.DBConnection;
import gov.nhso.prov.acc.utils.Utility;

public class ACC60105 extends Utility{
	public String url = "/apps/acc6/ACC60105A.jsp";

	public ACC60105(HttpServletRequest request, HttpServletResponse response) {
		this.message(request).clearMsg();
		String job = this.getAttr(request, "job");
		if(job.equals("init")){
			this.init(request);
		}else if(job.equals("saveProvince")){
			this.saveProvince(request);
		}else if(job.equals("editAccount")){
			this.editAccount(request);
		}
	}


	@SuppressWarnings("unchecked")
	private String init(HttpServletRequest request) {
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			List<Map<String, String>> list = null;
			
			Acc6ProvinceAccountBean provinceAccountBean = new Acc6ProvinceAccountBean();
			list = dao.selectList(provinceAccountBean);
			if(list.size() > 0){
				if(this.user(request).getAreaLevel().equals("3")){
					provinceAccountBean.setProvinceId(this.user(request).getAreaCode());
					list = dao.selectList(provinceAccountBean," ORDER BY PROVINCE_NAME");
				}else if(this.user(request).getAreaLevel().equals("2")){
					String condition = "";
					condition += " AND PROVINCE_ID IN (SELECT PROVINCE_ID FROM PROVINCE WHERE NHSO_ZONE = '" + this.user(request).getAreaCode()+ "' ) ";
					list = dao.selectList(provinceAccountBean, " "+ condition + " ORDER BY PROVINCE_NAME ASC ");
				}else{
					list = dao.selectList(provinceAccountBean," ORDER BY PROVINCE_NAME ASC ");
				}
				request.setAttribute("provinceAccountBean", list);
				request.setAttribute("job", "editAccount");
			}else{
				request.setAttribute("job", "saveProvince");
			}
			
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	@SuppressWarnings("unchecked")
	private String saveProvince(HttpServletRequest request){
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			List<Map<String, String>> list = null;
			Map<String, String> map = null;
			
			Acc6ProvinceAccountBean provinceAccountBean = new Acc6ProvinceAccountBean();
			list = this.dataUtil(dao).lsProvince();
			for(int i=0;i<list.size();i++){
				map = list.get(i);
				provinceAccountBean.setProvinceId(map.get("PROVINCE_ID"));
				provinceAccountBean.setProvinceName(map.get("PROVINCE_NAME"));
				provinceAccountBean.setActive("Y");
				provinceAccountBean.setCreatedBy(this.user(request).getUserId());
				provinceAccountBean.setUpdatedBy(this.user(request).getUserId());
				dao.insert(provinceAccountBean);
			}
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.init(request);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}

	private String editAccount(HttpServletRequest request) {
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			String[] provinceAccountDaily = request.getParameterValues("provinceAccountDaily");
			String[] provinceAccountAddress = request.getParameterValues("provinceAccountAddress");
			String[] provinceId = request.getParameterValues("provinceId");
			String[] groupWork = request.getParameterValues("groupWork");
			String[] telephoneNumber = request.getParameterValues("telephoneNumber");
			String[] faxNumber = request.getParameterValues("faxNumber");
			
			Acc6ProvinceAccountBean provinceAccountBean = new Acc6ProvinceAccountBean();
			for(int i=0;i<provinceAccountDaily.length;i++){
				provinceAccountBean = new Acc6ProvinceAccountBean();
				provinceAccountBean.setProvinceId(provinceId[i]);
				if (provinceAccountDaily[i] != null && !provinceAccountDaily[i].equals("")) {
					provinceAccountBean.setProvinceAccountDaily(provinceAccountDaily[i]);
				}else {
					provinceAccountBean.setProvinceAccountDaily("null");
				}
				if (provinceAccountAddress[i] != null && !provinceAccountAddress[i].equals("")) {
					provinceAccountBean.setProvinceAccountAddress(this.encodeUTF(provinceAccountAddress[i]));
				}else {
					provinceAccountBean.setProvinceAccountAddress("null");
				}
				if (groupWork[i] != null && !groupWork[i].equals("")) {
					provinceAccountBean.setGroupWork(this.encodeUTF(groupWork[i]));
				}else {
					provinceAccountBean.setGroupWork("null");
				}
				if (telephoneNumber[i] != null && !telephoneNumber[i].equals("")) {
					provinceAccountBean.setTelephoneNumber(telephoneNumber[i]);
				}else {
					provinceAccountBean.setTelephoneNumber("null");
				}
				if (faxNumber[i] != null && !faxNumber[i].equals("")) {
					provinceAccountBean.setFaxNumber(faxNumber[i]);
				}else {
					provinceAccountBean.setFaxNumber("null");
				}
				provinceAccountBean.setUpdatedBy(this.user(request).getUserId());
				provinceAccountBean.setUpdatedDate(this.sysDate);
				dao.update(provinceAccountBean);
			}
			this.message(request).setMsgCode("I002");
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.init(request);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}

}
